package day20180405;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestExcelStyle {

    public void excelStyle() throws IOException {

        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();

        //创建合并单元格对象
        CellRangeAddress rangeAddress = new CellRangeAddress(2,2,2,4);
        //括号内数据分别表示起始行，结束行，起始列，结束列

        //创建样式
        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);             //表示水平方向居中显示
        style.setVerticalAlignment(VerticalAlignment.CENTER);       //表示垂直方向居中显示

        //创建字体
        HSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short)16);
        //font.setFontHeight((short)320);           //效果一样，但值要设置为字体大小*20倍

        font.setColor(HSSFColor.GREEN.index);       //字体颜色设置为绿色
        font.setBold(true);
        style.setFont(font);

        //设置背景
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);         //设置背景样式
        style.setFillForegroundColor(HSSFColor.RED.index);              //设置背景颜色

        //创建工作表
        HSSFSheet sheet = workbook.createSheet("Helloworld");

        //添加合并区域
        sheet.addMergedRegion(rangeAddress);

        //创建行
        HSSFRow row = sheet.createRow(5);

        //创建单元格
        HSSFCell cell = row.createCell(3);
        cell.setCellValue("hello");
        cell.setCellStyle(style);

        //输出
        FileOutputStream outputStream = new FileOutputStream(new File("F:\\测试.xls"));
        workbook.write(outputStream);

        workbook.close();
        outputStream.close();

    }

}
